home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-04-18 | 4.9 KB | 145 lines | [TEXT/PJMM] |
- unit NextEvent;
- interface
- uses
- Globals, DoCommand, ReSize, UpdateWindow, HighLevelEvents, AppleEvents;
-
- procedure MainEventLoop;
-
- implementation
-
- {the main loop that handles events}
- {############################ MainEventLoop ##########################}
-
- { Brace yourself: here's where the action is. Most Mac programs just}
- { wait for events (as do we all), and then process them. There are}
- { two sorts of events: those directly initiated by the user, like key}
- { presses and mouse-downs, and those consequent events posted by the}
- { Event Manager, like update and activate events. The latter MUST be}
- { handled correctly and carefully. In particular, it's important for}
- { all events to make sure that the event occurred in or for the window}
- { you expect -- it's possible to get events which are not for one of}
- { our windows, despite GetNextEvent's return value. Similarly,}
- { be sure to check that the window it occured in is the active one,}
- { if it matters.}
-
- { A common mistake in handling update and activate events is in finding}
- { out which window they are for. It is "(WindowPtr)myevent.message "}
- { that gives this information, NOT "whichwindow" (the WindowPtr returned}
- { by FindWindow). The latter pointer merely tells you what window}
- { the mouse was in at the time the event was posted -- completely}
- { irrelevant for Update and Activate events. Think it through carefully.}
-
- procedure MainEventLoop;
- const
- kSleep = 5; {Should be a variable, the sleep time for WaitNextEvent.}
- var
- diskInitPt: Point;
- it: Longint;
- hasEvent: Boolean;
- begin
- (* body of MainEventLoop *)
-
- FlushEvents(everyEvent, 0);(* discard leftover events *)
-
- (* get next event, and Handle it appropriately, until user QUITs *)
-
- userdone := false;
- repeat
- {Get next event, with WNE if we can /LIR}
- if gWNEImplemented then
- hasEvent := WaitNextEvent(everyEvent, myevent, kSleep, nil)
- else
- begin
- SystemTask; (* Handle desk accessories *)
- hasEvent := GetNextEvent(everyEvent, myevent);
- end;
-
- if hasEvent then
- begin (* get event; if for us... *)
- case (myevent.what) of
- (* Handle each kind of event *)
-
- mouseDown:
- (* find out what window the mouse went down in, and where in it *)
- begin
- windowcode := FindWindow(myevent.where, whichwindow);
- case (windowcode) of
- (* Handle mouse-down for each place *)
- inSysWindow:
- (* Handle the desk accessories *)
- SystemClick(myevent, whichwindow);
- (* inSysWindow *)
- inMenuBar: (* Handle the command *)
- begin
- it := MenuSelect(myevent.where);
- userdone := DoCommand(it);
- {userdone := DoCommand(MenuSelect(myevent.where));}
- end;
- (* inMenuBar *)
- inDrag: (* drag the window *)
- DragWindow(whichwindow, myevent.where, dragrect);
- inContent:
- begin
- (* includes inGrow if window inactive.}
- { Activate window *)
- if (whichwindow = mywindow) then
- (* make sure it's for mine *)
- if (whichwindow <> FrontWindow) then
- SelectWindow(whichwindow);
- (* make it active *)
- end;
- inGrow:
- (* window is already active; change its size *)
- if (whichwindow = mywindow) then
- (* make sure it's for mine *)
- ReSize(mywindow, myevent.where);
- (* inGrow *)
- inGoAway:
- ;
- (* we don't have a GoAway Region *)
- end; (* case *)
- end; {mouseDown}
-
- keyDown, autoKey: (* if command key, pass the char to MenuKey *)
- if BitAnd(myevent.modifiers, cmdKey) <> 0 then
- userdone := DoCommand(MenuKey(char(BitAnd(myevent.message, charcodemask))));
- updateEvt: (* if it's for our window, update it *)
- if (WindowPtr(myevent.message) = mywindow) then
- UpdateWindow(mywindow);
- (* redraw the window contents *)
- activateEvt:
- (* if for our window, set port as nec. *)
- if (WindowPtr(myevent.message) = mywindow) then
- begin
- (* my window *)
- DrawGrowIcon(mywindow);
- (* redraw grow icon to reflect new state }
- if BitAnd(myevent.modifiers, 1) = 1 then
- (* odd means an activate event *)
- SetPort(mywindow)
- (* activate evt: work in our own port *)
- else
- SetPort(screenport);
- (* deactivate evt: our port is gone; keep port from dangling *)
- end; {activateEvt}
- diskEvt: {Handle uninitialized disks}
- if (HiWord(myevent.message) <> noErr) then
- begin
- DILoad;
- diskInitPt.v := 120;
- diskInitPt.h := 100;
- if DIBadMount(diskInitPt, myevent.message) <> noErr then
- ;
- DIUnload;
- end;{diskEvt, if}
- OSevt:
- DoHighLevelEvent(myevent); {Added by LIR}
- kHighLevelEvent:
- DoAppleEvent(myEvent); {Added by LIR}
- otherwise
- end; {case}
- end; {if GetNextEvent}
- until userdone;
- end;
-
- end.